import {
	defineStore
} from "pinia"

import {
	ref
} from "vue"

import {
	addPatientApi,
	patinetListApi,
	deletePatinetIdApi,
	patientDetailApi,
	updatePatientApi
} from "@/api/modules/patient.js"


const usePatientStore = defineStore("patient", () => {
	/* 患者列表 */
	const patientList = ref([])

	const addPatientApiAction = async (data) => {
		const res = await addPatientApi(data)
		if (res.code !== 10000) return uni.utils.toast(res.message)
		// 返回上一页
		uni.navigateBack()
	}


	const patinetListApiAction = async () => {
		const res = await patinetListApi()
		if (res.code !== 10000) return uni.utils.toast(res.message)
		patientList.value = res.data
		console.log(res.data);
	}
	/* 根据id删除患者 */
	const deletePatinetIdApiAction = async (id) => {
		const res = await deletePatinetIdApi(id)
		if (res.code !== 10000) return uni.utils.toast(res.message)
	}

	/* 获取患者详情信息 */

	const patientDetailApiAction = async (id) => {
		const {
			code,
			data,
			message
		} = await patientDetailApi(id);
		if (code !== 10000) return uni.utils.toast(message)
		console.log(data);
		return data
	}

	/* 更新患者信息 */

	const updatePatientApiAction = async (data) => {
		const res = await updatePatientApi(data)
		if (res.code !== 10000) return uni.utils.toast(res.message)
		// 返回上一页
		uni.navigateBack()
	}

	return {
		patientList,
		addPatientApiAction,
		patinetListApiAction,
		deletePatinetIdApiAction,
		patientDetailApiAction,
		updatePatientApiAction
	}
})

export default usePatientStore